<template>
  <div class="container">
    <div>
      <a-button type="primary" @click="addStudent">
        <template #icon><icon-plus-circle /></template>添加
      </a-button>
    </div>
    <a-table :bordered="{cell:true}" :pagination="false" stripe :data="state.stuObj.list">
      <template #columns>
        <a-table-column title="姓名" data-index="name"></a-table-column>
        <a-table-column title="年龄" data-index="age"></a-table-column>
        <a-table-column title="生日">
          <template #cell="{ record }"><DateFormat :value="record.birth" :format="'YYYY-MM-DD'"></DateFormat></template>
        </a-table-column>
        <a-table-column title="操作" width="350">
          <template #cell="{ record }">
            <a-space>
              <a-button type="primary" size="small" @click="viewStudent(record.id)"><template #icon><icon-eye /></template>查看</a-button>
              <a-button type="primary" size="small" @click="editStudent(record.id)"><template #icon><icon-edit /></template>修改</a-button>
              <a-popconfirm content="确认删除吗?" @ok="deleteStudent(record.id)">
                <a-button type="primary" status="danger" size="small"><template #icon><icon-delete /></template>删除</a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>
    <a-pagination class="mt10" show-total :total="state.stuObj.total" @change="loadStudent" @page-size-change="loadStudent" v-model:current="state.sd.pageNum" v-model:page-size="state.sd.pageSize"></a-pagination>

    <a-modal :title="state.popupTitle" v-model:visible="state.entityDetailVisible" :footer="false">
      <StudentDetail v-if="state.entityDetailVisible" v-model="state.entityDetailVisible" :entityId="state.popupEntityId"></StudentDetail>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import { Message } from '@arco-design/web-vue'
import mittBus from "@/utils/route-listener"
import {reactive, ref, onMounted, onUnmounted, defineAsyncComponent} from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const StudentDetail = defineAsyncComponent(() => import('@/views/basedata/student/studentDetail.vue'));

const state = reactive({
  sd: {
    pageNum: 1,
    pageSize: 5
  },
  stuObj: {},
  entityDetailVisible : false,
  popupTitle : '',
  popupEntityId : 0,
})
const loadStudent = ()=>{
  axios.post('/student/pagestudent', state.sd).then(res => {
    const { data } = res
    state.stuObj = data
  })
}
const addStudent = ()=>{
  //state.popupEntityId = Math.floor(Math.random() * 100000) * -1;
  state.popupEntityId = 0;
  state.popupTitle= "新增学生"
  state.entityDetailVisible= true
}
const editStudent = (id)=>{
  state.popupEntityId= id;
  state.popupTitle= "修改学生"
  state.entityDetailVisible= true
}
const viewStudent = (id)=>{
  router.push("/basedata/studentview?stuid=" + id)
}
const deleteStudent = (id)=>{
  axios.post('/student/deletestudent',{id : id}).then(res => {
    const { code, msg } = res
    if(code === '200') {
      Message.success(msg)
      loadStudent();
    }
  })
}
onMounted(()=>{
  loadStudent();
  mittBus.on("stuadd", ()=>{
    state.sd.pageNum = 1;
    loadStudent();
  })
  mittBus.on("stuedit", ()=>{
    loadStudent();
  })
})
onUnmounted(()=>{
  mittBus.off("stuadd", ()=>{})
  mittBus.off("stuedit",()=>{})
})
</script>

<style scoped>

</style>
